home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6826 / 6826.xpi / content / gadrm.js < prev    next >
Text File  |  2009-03-09  |  9KB  |  268 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  * 
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is GAds remover.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Ognean Dragos.
  18.  * Portions created by the Initial Developer are Copyright (C) 2008
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  * 
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. function GadrmGeneralWrapper(){}
  38. GadrmGeneralWrapper.isException=false;
  39.  
  40. var appEnviron=
  41. {
  42.     enabled:true,
  43.     removeAnalytics:true,
  44.     exceptions:null,
  45.     init:function()
  46.     {
  47.          var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  48.                                 .getService(Components.interfaces.nsIPrefService);
  49.          var br = prefService.getBranch("extensions.gadrm.");
  50.          //br.QueryInterface(Components.interfaces.nsIPrefBranch2);                 
  51.          this.enabled=br.getBoolPref("remove_enable");
  52.          this.removeAnalytics=br.getBoolPref("remove_analytics");   
  53.          var except=gAdsRemoveUtils.getPreference("exception_list");         
  54.            if(except!="")
  55.            {
  56.                this.exceptions=except.split(",");
  57.            }else
  58.            {
  59.                this.exceptions=null;
  60.            }                
  61.     },
  62.     isException:function(url)
  63.      {              
  64.           if(this.exceptions==null)return false;               
  65.           for(var i=0;i<this.exceptions.length;i++)
  66.           {                  
  67.               var expr = this.exceptions[i];
  68.             expr = expr.replace(/\./g, "\\.");
  69.             expr = expr.replace(/\-/g, "\\-");
  70.             expr = expr.replace(/\*/g, "[A-Za-z0-9_\\-\\.]*");
  71.             //expr = expr + "$";            
  72.             var re = new RegExp(expr);
  73.             if(re.test(url))
  74.                 return true;    
  75.           }
  76.           return false;
  77.     }
  78. }
  79. appEnviron.init();
  80. GadrmGeneralWrapper.appEnv=appEnviron;
  81.  
  82. var winObserver=
  83. {        
  84.     tabBrowser:null,
  85.     register:function()
  86.     {           
  87.         this.tabBrowser=$("content");        
  88.         this.tabBrowser.addProgressListener(this);                
  89.     },
  90.     unregister:function()
  91.     {
  92.         this.tabBrowser.removeProgressListener(this);
  93.     },
  94.     onStateChange:function(aWebProgress,aRequest,aStateFlags,aStatus)
  95.     {
  96.         return;
  97.     },
  98.     onProgressChange:function(aWebProgress,aRequest,aCurSelfProgress,aMaxSelfProgress,aCurTotalProgress,aMaxTotalProgress)
  99.     {
  100.         return;
  101.     },
  102.  
  103.     onLocationChange:function(aWebProgress,aRequest,aLocation)
  104.     {
  105.         // Only watch windows that are their own parent - e.g. not frames
  106.         if (aWebProgress.DOMWindow.parent == aWebProgress.DOMWindow)
  107.         {            
  108.             if(!appEnviron.isException(aWebProgress.DOMWindow.location))
  109.             {                   
  110.                 //requestObserver.register();
  111.                 //GadrmGeneralWrapper.gadrm.enable();
  112.                 GadrmGeneralWrapper.isException=false;
  113.             }
  114.             else
  115.             {                                
  116.                 //GadrmGeneralWrapper.gadrm.disable();
  117.                 GadrmGeneralWrapper.isException=true;
  118.             }
  119.             var win=aWebProgress.DOMWindow;
  120.             /*if(appEnviron.removeAnalytics)
  121.                 gAdsRemoveUtils.insertJsVar(win,"var urchinTracker=function(){}");*/                                              
  122.         } 
  123.     },
  124.     onStatusChange:function (aWebProgress,aRequest,aStatus,aMessage)
  125.     {
  126.         return;
  127.     },
  128.     onSecurityChange:function(aWebProgress,aRequest,aState)
  129.     {
  130.         return;
  131.     }
  132.    
  133. }
  134. GadrmGeneralWrapper.winObs=winObserver;
  135.  
  136. //channer observer; monitor the URL that are called from whithin the browser
  137. var requestObserver={
  138.     register:function()
  139.     {        
  140.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  141.         observerService.addObserver(this,"http-on-modify-request", false);
  142.     },
  143.     unregister:function()
  144.     {
  145.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  146.         observerService.removeObserver(this, "http-on-modify-request");
  147.     },
  148.     observe:function(aSubject, aTopic, aData)    
  149.     {        
  150.         if(GadrmGeneralWrapper.isException)return;        
  151.         if(aTopic=="http-on-modify-request")
  152.         {
  153.             try
  154.             {
  155.                 aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
  156.                 if(gAdsRemoveUtils.isAd(aSubject.URI.asciiSpec))
  157.                 {
  158.                     //the request is dropped
  159. //                    aSubject.cancel(Components.results.NS_BINDING_ABORTED);
  160.                     aSubject.cancel(Components.results.NS_ERROR_NO_CONTENT );
  161.                     gAdsRemoveUtils.log(" --- Blocked->"+aSubject.URI.asciiSpec);                                
  162.                 }
  163.             }catch(e){    gAdsRemoveUtils.log("ERROR" + e.message);}                
  164.         }
  165.     }
  166. };
  167.  
  168.  
  169. //preferences observer
  170. var myPrefObserver =
  171. {  
  172.   register: function()
  173.   {
  174.     var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  175.                                 .getService(Components.interfaces.nsIPrefService);
  176.     this._branch = prefService.getBranch("extensions.gadrm.");
  177.     this._branch.QueryInterface(Components.interfaces.nsIPrefBranch2);
  178.     this._branch.addObserver("", this, false);
  179.   },
  180.  
  181.   unregister: function()
  182.   {
  183.     if(!this._branch) return;
  184.     this._branch.removeObserver("", this);
  185.   },
  186.  
  187.   observe: function(aSubject, aTopic, aData)
  188.   {
  189.     if(aTopic != "nsPref:changed") return;
  190.     // aSubject is the nsIPrefBranch we're observing (after appropriate QI)
  191.     // aData is the name of the pref that's been changed (relative to aSubject)        
  192.     switch(aData)
  193.     {
  194.         case "remove_enable":          
  195.             GadrmGeneralWrapper.appEnv.enabled=this._branch.getBoolPref("remove_enable");            
  196.             if(GadrmGeneralWrapper.appEnv.enabled)
  197.             {                
  198.                 GadrmGeneralWrapper.gadrm.enable();
  199.             }               
  200.             else
  201.             {                
  202.                 GadrmGeneralWrapper.gadrm.disable();
  203.             }               
  204.         break;
  205.         case "remove_analytics":            
  206.             GadrmGeneralWrapper.appEnv.removeAnalytics=this._branch.getBoolPref("remove_analytics");            
  207.         break;
  208.         default:            
  209.             GadrmGeneralWrapper.appEnv.init();          
  210.     }
  211.   }
  212. }
  213. GadrmGeneralWrapper.prefObs=myPrefObserver;
  214.  
  215. var gadrm = {
  216.   //init the app 
  217.   
  218.   init: function() {
  219.       GadrmGeneralWrapper.prefObs.register();
  220.       if(!appEnviron.enabled)return;      
  221.     var appcontent = document.getElementById("appcontent");   // browser    
  222.     if(appcontent)
  223.     {      
  224.       // requestObserver.register();    
  225.       //appcontent.addEventListener("DOMContentLoaded", this.onPageLoad, true);      
  226.       appcontent.addEventListener("DOMFrameContentLoaded", this.onFrameLoad, true);     
  227.       this.enable();              
  228.     }
  229.   },  
  230.   onFrameLoad: function(aEvent) {      
  231.      if(GadrmGeneralWrapper.isException)return;
  232.      var frame = aEvent.originalTarget;          
  233.      if(gAdsRemoveUtils.isAd(frame.src) && appEnviron.enabled)
  234.      {
  235.         var tmpelem=frame.contentDocument.createElement("div");
  236.         tmpelem.style.backgroundImage="url(chrome://gadrm/content/gadrm.png)";
  237.         tmpelem.style.backgroundPosition="left";
  238.         tmpelem.style.backgroundAttachment="scroll";
  239.         tmpelem.style.backgroundRepeat="no-repeat";
  240.         tmpelem.style.padding="2px 2px 2px 32px";
  241.         tmpelem.style.lineHeight="32px";
  242.         tmpelem.style.height="32px";
  243.         if(frame.parentNode)tmpelem=frame.parentNode.replaceChild(tmpelem,frame);
  244.         /*var docRef=frame.contentDocument.getElementsByTagName("body").item(0);        
  245.         docRef.innerHTML="<strong style=\"font-size:13px;\"><img src=\"chrome://gadrm/content/gadrm.png\" align=\"absmiddle\" /> Adds blocked</strong>";*/
  246.      }
  247.   },
  248.   onPageLoad: function(aEvent) {
  249.         var doc = aEvent.originalTarget;
  250.   },
  251.   enable:function()
  252.   {    
  253.      winObserver.register();
  254.      //myPrefObserver.register();
  255.      requestObserver.register();    
  256.   },
  257.   disable:function()
  258.   {
  259.        winObserver.unregister();
  260.        //myPrefObserver.unregister();       
  261.        requestObserver.unregister();
  262.   }       
  263. };
  264.  
  265. GadrmGeneralWrapper.gadrm=gadrm;
  266.  
  267. window.addEventListener("load", function() { gadrm.init(); }, false);
  268.